<?php
namespace app\index\controller;
use think\Request;
use think\Db;
use think\Controller;
use app\index\model\User as User;
use think\Session;
class User extends Controller
{
    // 用户中心
    public function user(Request $req)
    {
      $username = session('username');
      $picture  = session('picture');
      $this->assign('username',$username);
      $this->assign('picture',$picture);
		  return $this->fetch();
    }
    public function address( Request $req)//地址管理
    {
      $user = new User;
      $username = session('username');
      $receiver = $req->param('username');
      $uid = $user->getUid($username);
      $uid = $uid[0]->uid;
      $addinfo = $user->getAdd($uid);
      foreach ($addinfo as $key => $value) {
         $num = $addinfo[$key]['province'];
         $addinfo[$key]['province']=$user->province()[$num];
         $addinfo[$key]['city']=$user->city()[$num];
         $addinfo[$key]['country']=$user->country()[$num];
         /*dump($addinfo[$key]['province']);die;*/
      }
      $this->assign('username',$username);
      $this->assign('addinfo',$addinfo);
		  return $this->fetch();
    }
    public function adds(Request $req)//增加地址
    {
      $user = new User;
      $username = session('username');
      $receiver = $req->param('username');
      $uid = $user->getUid($username);
      $uid = $uid[0]->uid;
      $phone     = $req->param('phone');
      $province  = $req->param('province');
      $city      = $req->param('city');
      $country   = $req->param('country');
      $details   = $req->param('details');
      $data = [
        'receiver'  => $receiver,
        'province'  => $province,
        'phone'     => $phone,
        'city'      => $city,
        'country'   => $country,
        'address'   => $details,
        'uid'       => $uid,
        'create_time' =>time()
        ];
      
      $result = $user->add($data);
      if($result){

        return true;
      }else{
        return false;
      }
    }
    public function delAdd(Request $req)//删除地址
    {

      $aid = $req->param('aid');
      $user = new User;
      $result = $user->delAdd($aid);
      return $result;
    }
    public function bill( )//账单管理
    {
		  return $this->fetch();
    }
    public function billlist( )//
    {
		  return $this->fetch();
    }
 	  public function bindphone( )
    {
		  return $this->fetch();
    }
    public function bonus( )
    {
      return $this->fetch();
    }
    public function cart()
    {
      return $this->fetch();
    }
    public function cardlist( )//地址管理
    {
      return $this->fetch();
    }
      public function cardmethod( )//账单管理
    {
    return $this->fetch();
    }
    public function commentlist(Request $req)//评论列表
    {
      $user = new User;
      $pid = $req->param('pid');
      session('pinglun',$pid);
      $pinfo = $user->getProduct($pid);
      /*dump($pinfo);die;*/
      $img = $user->getImage($pid);
      $img = $img['image'];
      $this->assign('pid',$pid);
      $this->assign('pinfo',$pinfo);
      $this->assign('img',$img);
      return $this->fetch();
    }
    public function addCom(Request $req)//增加评论
    {
      $user = new User;
      $pid = session('pinglun');
      $username = session('username');
      $uid = $user->getUid($username);
      $uid = $uid[0]->uid;//用户Id
      $point = $req->param('point');
      $content = $req->param('content');
      $data = [
        'point'  =>$point,
        'content'=>$content,
        'uid'    =>$uid,
        'pid'    =>$pid
      ];
     $result = $user->addCom($data);
     if($result ==1){
      $this->success('发表成功');
     }else{
      $this->error('发表失败');
     }
    }
    public function comment( )//评论管理
    {
      $user = new User;
      $username = session('username');
      $uid = $user->getUid($username);
      $uid = $uid[0]->uid;//用户Id
      $com = $user->getCom($uid);
      foreach ($com as $key => $value) {
        $pid =$com[$key]['pid'];
        $product = $user->getPro($pid);
        $img = $user->getImage($pid);
         // dump($img['image']);die;
        $com[$key]['image'] = $img['image'];
        $com[$key]['name']  = $product[0]['name'];
        $com[$key]['product_number']  = $product[0]['product_number'];
        // dump($product);die;
      }
      $this->assign('com',$com);
      return $this->fetch();
    }
      public function change( )//
    {
    return $this->fetch();
    }
    public function collection( )//收藏
    {
      $uid = session('uid');
      $user = new User();
      $col = $user->getCollection($uid);
      foreach ($col as $key => $value) {
         $pid = $value['pid'];
         $product = $user->getProduct($pid);
         $img = $user->getImage($pid);
        // dump($product['name']);
         $col[$key]['name'] = $product['name'];
         $col[$key]['now_money'] = $product['now_money'];
         $col[$key]['original_money'] = $product['original_money'];
         $col[$key]['inventory'] = $product['inventory'];
         $col[$key]['image'] = $img['image'];
      }
      $this->assign('col',$col);
      return $this->fetch();
    } 
    public function deteleCol(Request $req)//删除收藏商品
    {
      $pid = $req->param('pid');
      $user = new User;
      $result = $user->deteleCol($pid);
      return $result;
    }
    public function consultation(Request $req )//咨询问题页面
    {
      return $this->fetch();
    }
    public function consul(Request $req)//提交咨询问题
    {
      $user = new User;
      $username = session('username');
      $uid = $user->getUid($username);
      $uid = $uid[0]->uid;//用户Id
      $num = $req->post('question');
      $content = $req->post('opinionContent');
      /*$question = $user->question()[$num];*/
      $data=[
        'uid'          => $uid,
        'advisory_type'=> $num,
        'content'      => $content,
        'create_time'  => time()
      ];
      $result = $user->addSul($data);
      if($result){
        $this->success('发表成功');
      }else{
        $this->error('发表失败');
      }
    }
    public function contact( )
    {
      return $this->fetch();
    }
    public function coupon( )//优惠券管理
    {
      $user = new User;
      $username = session('username');
      $uid = $user->getUid($username);
      $uid = $uid[0]->uid;//用户Id
      $type = $user->getType($uid);
      foreach ($type as $key => $value) {
        $typeid =$type[0]->type_id;
        $info = $user->getCoupon($typeid);
        $type[$key]['face_value'] = $info[0]->face_value;
        /*dump($type[$key]['face_value']);die;*/
        $type[$key]['full_money'] = $info[0]->full_money;
        $type[$key]['describe'] = $info[0]->describe;
        $type[$key]['begin_time'] = $info[0]->begin_time;
        $type[$key]['end_time'] = $info[0]->end_time;
        $type[$key]['number'] = $info[0]->number;
        $type[$key]['is_enable']= $info[0]->is_enable;
        $type[$key]['id']= $info[0]->id;
        $num = $type[$key]['type_id'];
        $type[$key]['type_id'] = $user->type()[$num];
       /* dump($type[$key]['type_id']);die;*/
      }
        
      $this->assign('type',$type);
      return $this->fetch();
    }
    public function nowUse(Request $req)//领取优惠券
    {
      $user= new User;
      $id = $req->param('id');
      $data=['is_enable'=>2];
      $result = $user->updateAble($id,$data);
      return $result;
    }
    public function email( )//账单管理
    {
      return $this->fetch();
    }
      public function foot( )//足迹
    {
      $user = new User;
      $username = session('username');
      $uid = $user->getUid($username);
      $uid = $uid[0]->uid;//用户Id
      $product = $user->getCol($uid);//获取足迹表数据
      foreach ($product as $key => $value) {
       $pname = $product[$key]->name;//商品名字
       $info  = $user->getProInfo($pname);//商品信息
       $pid = $info[0]->pid;//商品ID
       $img   = $user->getProImg($pid);//商品图片
       $product[$key]['name']              = $info[0]['name'];
       $product[$key]['now_money']         = $info[0]['now_money'];
       $product[$key]['original_money']    = $info[0]['original_money'];
       $product[$key]['inventory']         = $info[0]['inventory'];
       $product[$key]['is_on']             = $info[0]['is_on'];
       $product[$key]['image']             = $img[0]['image'];
       $product[$key]['pid']               = $info[0]['pid'];
      }
      $this->assign('product',$product);
      return $this->fetch();
    }
    public function deteleFoot(Request $req)//删除足迹商品
    {
      $user = new User;
      $pid = $req->param('pid');
      $name = $user->getName($pid);
      $name = $name[0]->name;
      $result = $user->deteleFoot($name);
      return $result;
    }
    public function idcard( )
    {
     return $this->fetch();
    }
      public function inform()//个人信息
    {
      $username = session('username');
      $this->assign('username',$username);
      $user = new User;
      $result = $user->getInfo($username);
      //return $result;
      $this->assign('result',$result);
      return $this->fetch();
    }
    public function update(Request $req)//修改个人资料
    {
        $username = session('username');
        $user = new User;
        $data = $req->param();
        $img = $this->upload();
        //return $img;
        if(!$img){
            return false;
        }
        $data['picture'] = $img;
        $result = $user->updateInfo($username,$data);
        //return $result;
        if($result){
           session('picture',$img);
           session('phone',$data['phone']);
          $this->success('修改成功');
         
        }else{
          $this->error('修改失败');
        }
    }
    public function upload()//上传头像
    {
      $file = request()->file('picture');
      $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
      if($info){
        $fileinfo = $info->getSaveName();
        return $fileinfo ='uploads/'.str_replace('\\', '/', $fileinfo);
      }else{
          return false;
      }
    }
    public function logistics( )//地址管理
    {
      return $this->fetch();
    }
    public function news( )//账单管理
    {
      return $this->fetch();
    }
    public function order(Request $req)//订单管理
    {
      $user = new User;
      $username = session('username');
      $uid = $user->getUid($username);
      $uid = $uid[0]->uid;//用户Id
      $order = $user->getOrder($uid);
      
      foreach ($order as $key => $value) {
        $pid = $order[$key]->pid;
        $pinfo = $user->getOrderInfo($pid);
        $img   = $user->getProImg($pid);
        $num = $order[$key]['status'];
        $order[$key]['name']      = $pinfo[0]['name'];
        $order[$key]['now_money'] = $pinfo[0]['now_money'];
        $order[$key]['status'] = $user->status()[$num];
        $order[$key]['image'] = $img[0]['image'];
        /*dump($order[$key]['image']);die;*/

      }
      // dump($order);die;
      $this->assign('order',$order);
      return $this->fetch();
    }
    public function delOrder(Request $req)
    {
      $user = new User;
      $oid = $req->param('oid');
      $result = $user->deteleOrder($oid);
      return $result;
    }
    public function orderinfo( )
    {
      return $this->fetch();
    }
      public function password( )
    {
    return $this->fetch();
    }
    public function updatePassword(Request $req)//修改密码
    {
      $user = new User;
      $username = session('username');
      $password = session('password');
      $pass = md5($req->param('password'));
      $renew = md5($req->param('renewpassword'));
      if($pass == $password)
      {
        if($renew == $newpass){
          $data=['password'=>$renew];
          $result = $user->updatePass($username,$data);
          if($result){
            $this->success('密码修改成功','index/user/user');
          }else{
            $this->error('密码修改失败');
          }
        }else{
          return false;
        }
      }else{
        return false;
      }
    }
      public function pointnew( )//地址管理
    {
    return $this->fetch();
    }
      public function points( )//账单管理
    {
    return $this->fetch();
    }
      public function question( )//
    {
    return $this->fetch();
    }
    public function record( )
    {
    return $this->fetch();
    }
      public function refund( )
    {
    return $this->fetch();
    }
      public function safety( )//地址管理
    {
      $this->assign('username',session('username'));
      $this->assign('picture',session('picture'));
      return $this->fetch();
    }
    public function setpay()//设置支付密码
    {

      $this->assign('phone',session('phone'));
      return $this->fetch();
    }
    public function send(Request $req)//手机验证
    {
      //$tel = $req->param('phone');
      $options['accountsid']='55b4800138b28f1066e15272a16702aa';
      $options['token']='bd3ca532c13c994977b6f1abe2c1a03a';
      $phpone = new \my\Phone($options);
      $appId = "210fd2a99496497caca8d49475bba568";//当前应用
      $to = '15030893082';
      $templateId = "44080";//模板ID
      $param="3214";
      
     $result = $phpone->templateSMS($appId,$to,$templateId,$param);
      if($result){
        session('code',$param);
        return true;
      }else{
        return false;
      }
    }
    public function check(Request $req)//验证手机短信
    {
      $code = $req->param('code');
      if($code == session('param')){
        return true;
      }else{
        return false;
      }
    }
    public function compared(Request $req)//支付密码确认
    {
      $user = new User();
      $username = session('username');
      $pay = md5($req->param('pay'));
      //dump($pay);
      $repay = md5($req->param('repay'));
     // dump($repay);
      if($repay == $pay){
        $data = ['pay_password'=>$repay];
        return $user->payPassword($username,$data);
        /*if($result){
            $this->success('密码设置成功','index/user/user');
          }else{
            $this->error('密码设置失败');
          }*/
      }else{
        return false;
      }

    }
      public function suggest( )//账单管理
    {
    return $this->fetch();
    }
      public function wallet( )//
    {
    return $this->fetch();
    }
    public function walletlist( )
    {
    return $this->fetch();
    }

}
